﻿using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Tools
{
   
    public class RestHelper
    {
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="url"></param>
        /// <param name="filesrc"></param>
        /// <returns></returns>
        public static object PostFilebyUrl(string url, string filesrc)
        {
            var client = new RestClient(url);
            var request = new RestRequest("", Method.POST);
            request.Timeout = 300 * 1000; //设置超时时间3分钟 
            //txtPictures.Text为文件路径
            request.AddFile("file", filesrc);
            RestResponse rts = (RestResponse)client.Execute(request);

            if (rts.StatusCode != System.Net.HttpStatusCode.OK)
            {
                throw new Exception("网络异常" + rts.StatusCode + rts.StatusCode);
            }
            dynamic result = Newtonsoft.Json.JsonConvert.DeserializeObject(rts.Content);
            return result;
        }

        /// <summary>
        /// post 上传文件流
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="url"></param>
        /// <param name="filename"></param>
        /// <param name="postfile"></param>
        /// <returns></returns>
        public static T PostFilebyte<T>(object data, string url, string filename, Byte[] postfile) where T : new()
        {
            var client = new RestClient(url);
            var request = new RestRequest("", Method.POST);
            request.Timeout = 300 * 1000; //设置超时时间3分钟
            request.AddParameter("projectId", "F25F57FC-DD28-4349-AFAD-5797D6B3AF06");
            //txtPictures.Text为文件路径

            request.AddFile("file", postfile, filename);
            RestResponse response = (RestResponse)client.Execute(request);
            if (response.StatusCode != HttpStatusCode.OK)
            {

                throw new Exception("网络异常" + response.StatusCode + response.Content);
            }
            // curentJson = response.Content;  //记录数据，以便回滚
            T result = JsonConvert.DeserializeObject<T>(response.Content);
            return result;
        }

        public static T GeteByT<T>(string url)
        {
            var client = new RestClient(url);
            var request = new RestRequest("", Method.GET);
            request.Timeout = 300 * 1000; //设置超时时间3分钟 
            //txtPictures.Text为文件路径
            RestResponse rts = (RestResponse)client.Execute(request);

            if (rts.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception("网络异常" + rts.StatusCode + rts.Content);
            }

            T result = JsonConvert.DeserializeObject<T>(rts.Content);
            return result;
        }

        public static string Get(string url)
        {
            var client = new RestClient(url);
            var request = new RestRequest("", Method.GET);
            request.Timeout = 300 * 1000; //设置超时时间3分钟 
            //txtPictures.Text为文件路径
            RestResponse rts = (RestResponse)client.Execute(request);

            if (rts.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception("网络异常" + rts.StatusCode + rts.Content);
            }

            //dynamic result = Newtonsoft.Json.JsonConvert.DeserializeObject(rts.Content);
            return rts.Content;
        }

        /// <summary>
        ///  post请求
        /// </summary>
        /// <param name="postdata"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public static dynamic Post(object postdata, string url)
        {
            var client = new RestClient(url);
            var request = new RestRequest("", Method.POST);
            request.Timeout = 300 * 1000; //设置超时时间5分钟 
            if (postdata != null)
            {
                var json = Newtonsoft.Json.JsonConvert.SerializeObject(postdata);
                request.AddParameter("application/json; charset=utf-8", json, ParameterType.RequestBody);
            }
            RestResponse response = (RestResponse)client.Execute(request);
            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception("网络异常" + response.StatusCode + response.Content);
            }
            var content = response.Content; // raw content as string
            return content;

        }

        /// <summary>
        /// post 请求接口获得对象
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static T Post<T>(object data, string url) where T : new()
        {
            var client = new RestClient(url);
            var request = new RestRequest("", Method.POST);
            request.Timeout = 300 * 1000; //设置超时时间3分钟

            string postJson = "{}";
            if (data != null && data.ToString() != "")
            {

                postJson = Newtonsoft.Json.JsonConvert.SerializeObject(data);
            }

            request.AddParameter("application/json; charset=utf-8", postJson, ParameterType.RequestBody);
            RestResponse response = (RestResponse)client.Execute(request);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                   throw new Exception("网络异常" + response.StatusCode + response.Content);
            }
            string resultstr = response.Content.Replace(@"\u0000", "").Replace("[[]]", "[]");
            T result = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(resultstr);
            return result;

        }


        /// <summary>
        ///  post请求
        /// </summary>
        /// <param name="postdata"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public static dynamic Post(string postjson, string url)
        {
            var client = new RestClient(url);
            var request = new RestRequest("", Method.POST);
            request.Timeout = 300 * 1000; //设置超时时间5分钟 
            if (postjson != null)
            {

                request.AddParameter("application/json; charset=utf-8", postjson, ParameterType.RequestBody);
            }
            RestResponse response = (RestResponse)client.Execute(request);
            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception("网络异常" + response.StatusCode + response.Content);
            }
            var content = response.Content; // raw content as string
            return content;

        }


    
        /// <summary>
        /// 上传文件 和数据
        /// </summary>
        /// <param name="url">url地址</param>
        /// <param name="filesrc">本地文件路径</param>
        /// <param name="reqdataName">参数名称 pdf_pages</param>
        /// <param name="reqdata">请求数据</param>
        /// <returns></returns>
        public static T PostFileAndData<T>(string url, string filesrc, string reqdataName, object reqdata)
        {
            var client = new RestClient(url);
            var request = new RestRequest("", Method.POST);
            request.Timeout = 300 * 1000; //设置超时时间3分钟 
            request.AddFile("pdf_file", filesrc);
            var json = Newtonsoft.Json.JsonConvert.SerializeObject(reqdata);
            request.AddParameter(reqdataName, json, "application/json; charset=utf-8", ParameterType.RequestBody);

            RestResponse response = (RestResponse)client.Execute(request);

            if (response.StatusCode != System.Net.HttpStatusCode.OK)
            {
                throw new Exception("网络异常" + response.StatusCode + response.Content);
            }
            T result = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(response.Content);
            return result;

        }


    }

}
